package com.atguigu.imperial.court.service.impl;

import com.atguigu.imperial.court.court.util.ImperialCourtConst;
import com.atguigu.imperial.court.court.entity.Emp;
import com.atguigu.imperial.court.court.exception.LoginFailedException;
import com.atguigu.imperial.court.mapper.EmpMapper;
import com.atguigu.imperial.court.service.api.EmpService;
import com.atguigu.imperial.court.court.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * ClassName: EmpServiceImpl
 * Description:
 * Date: 2022/09/20 PM 11:33
 * <p>
 * project: pro07-demo-imperial-court-micro-service
 * package: com.atguigu.imperial.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public Emp getEmpByLoginInfo(String loginAccount, String loginPassword) {
        String encodedLoginPassword = MD5Util.encode(loginPassword);
        Example example = new Example(Emp.class);
        example
                .createCriteria()
                .andEqualTo("loginAccount", loginAccount)
                .andEqualTo("loginPassword", encodedLoginPassword);
        List<Emp> empList = empMapper.selectByExample(example);
        if (empList == null || empList.size() == 0) {
            throw new
                    LoginFailedException(ImperialCourtConst.LOGIN_FAILED_MESSAGE);
        }
        return empList.get(0);
    }

}
